package navigation.wzg.com.navigation.activities;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import navigation.wzg.com.navigation.MainActivity;
import navigation.wzg.com.navigation.R;
import navigation.wzg.com.navigation.Welcome;
import navigation.wzg.com.navigation.Write.Shareds;
import navigation.wzg.com.navigation.http.Http_method;

public class Activity_menu_login extends AppCompatActivity {


    @BindView(R.id.edit_info)
    EditText editInfo;
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    @BindView(R.id.button_info)
    Button buttonInfo;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.tiaoyue_text)
    TextView tiaoyueText;
    @BindView(R.id.toolbar_login)
    Toolbar toolbarLogin;
    @BindView(R.id.activity_login)
    LinearLayout activityLogin;
    @BindView(R.id.register)
    Button register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_login);
        ButterKnife.bind(this);
        toolbarLogin.setTitle("登陆");

        if (Shareds.Firsttrue("checkboxture",Activity_menu_login.this)){
            String[] aa = Shareds.Reads(Activity_menu_login.this);
            editInfo.setText(aa[0]);
            editPwd.setText(aa[1]);
            if (aa.length>0){
                checkbox.setChecked(true);
            }else {
                checkbox.setChecked(false);
            }
        }

    }

    /**
     * 监听返回按键
     */
    public void onBackPressed() {
        exit();
    }


    /**
     * 黄油刀点击事件
     */
    @OnClick({R.id.button_info, R.id.checkbox, R.id.tiaoyue_text, R.id.register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register:
                startActivity(new Intent(Activity_menu_login.this, Register.class));
                overridePendingTransition(R.anim.out, R.anim.up);
                break;
            case R.id.button_info:
                login11();
                if (checkbox.isChecked()){
                    Shareds.Writes(editInfo.getText().toString().trim(),
                            editPwd.getText().toString().trim(),
                            Activity_menu_login.this);
                    Shareds.First("checkboxture",true,Activity_menu_login.this);
                }else{
                    Shareds.Writes("","", Activity_menu_login.this);
                    Shareds.First("checkboxture",false,Activity_menu_login.this);
                }
                break;
            case R.id.tiaoyue_text:
                new AlertDialog.Builder(this)
                        .setTitle("注意！！！")
                        .setMessage("本程序仅用于毕业设计使用，所有包括云端的账号密码及传输都未经过加密处理，请勿用来保存重要文件！造成损失后果自负")
                        .setPositiveButton("同意", null)
                        .show();
                break;
        }
    }
    public void login11() {
        if (editInfo.getText().length()<=0||editPwd.getText().length()<=0){
            Toast.makeText(this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
        }else {
                int a= Http_method.state(editInfo.getText().toString(),
                                    editPwd.getText().toString(),
                                    Activity_menu_login.this);
            if (a == 200){
                startActivity(new Intent(Activity_menu_login.this, MainActivity.class));
                Shareds.Writes_username(editInfo.getText().toString(),this);
                exit();
            }else {
                startActivity(new Intent(Activity_menu_login.this, MainActivity.class));
                Shareds.Writes_username(editInfo.getText().toString(),this);
                exit();
                Toast.makeText(this, "账号或密码错误\n登录前请务必确认网络连接", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void exit() {
        Welcome.ActivityA.finish();
        finish();
        overridePendingTransition(R.anim.out, R.anim.up);
    }
}
